package com.bye.config;

import com.bye.intercept.AuthIntercept;
import com.bye.resolver.AccountHandlerMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private AccountHandlerMethodArgumentResolver accountHandlerMethodArgumentResolver;

    @Autowired
    private AuthIntercept authIntercept;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //需要处理跨域问题的请求地址
        registry.addMapping("/**")
                .allowedMethods("*")//允许的请求方式
                .allowedOriginPatterns("*")//允许的请求客户端
                .allowedHeaders("*")//允许携带的请求头
                .allowCredentials(true)//允许携带拼争
                .maxAge(36000);//最大等待时间
    }

    @Override
    //配置拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authIntercept).addPathPatterns("/**");
    }

    @Override
    //配置方法参数解析器, 让其生效
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(accountHandlerMethodArgumentResolver);
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
    }
}
